package com.dragon.www.haier.model.xml;

import android.util.Log;

import com.dragon.www.haier.net.Utils;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


/**
 *	操作指令处理入口
 * Created by Administrator on 2015/11/20 16:09.
 * Author: songxy
 * Email: thinkdata@163.com
 */
public class CodeXmlRootHandler extends DefaultHandler {

	private static final String TAG = "CodeXmlRootHandler";

	private List elementNode = new ArrayList();

	private ArrayList<Dictionary> data = new ArrayList<>();

	private List<DictionaryItem> items = new ArrayList<>();

	private List<DictionaryItemField> fields = new ArrayList<>();

	private List<DictionaryGroupItem> groupItems = new ArrayList<>();

	private Stack<Dictionary> dictionaryStack = new Stack<Dictionary>();

	private Stack<DictionaryItem> dictionaryItemsStack = new Stack<DictionaryItem>();

	private Stack<DictionaryGroupItem> dictionaryGroupItemStack = new Stack<DictionaryGroupItem>();

	private Stack<DictionaryItemField> dictionaryItemFieldsStack = new Stack<>();

	private Stack<String> qNameStack = new Stack<>();

	@Override
	public void error(SAXParseException var1) {
		Log.e(TAG, "Error: " + Log.getStackTraceString(var1));
	}

	@Override
	public void fatalError(SAXParseException var1) {
		Log.e(TAG, "FatalError: " + Log.getStackTraceString(var1));
	}

	@Override
	public void warning(SAXParseException var1) {
		Log.e(TAG, "Warning: " + Log.getStackTraceString(var1));
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		System.out.println(qNameStack.push(qName));
		if (qName.equals("dictionaries")) {

		}else if (qName.equals("dictionary")) {
			Dictionary dictionary = new Dictionary();
			if (attributes != null && attributes.getValue("type") != null) {
				dictionary.setType(attributes.getValue("type"));
			}
			dictionaryStack.push(dictionary);
		}else if (qName.equals("group")) {
			DictionaryGroupItem groupItem = new DictionaryGroupItem();
			groupItem.setGroupName(attributes.getValue("name"));
			String length = attributes.getValue("length");
			if (length != null) {
				groupItem.setGroupActionLength(Integer.parseInt(length));
			}
			String value = attributes.getValue("value");
			if (value != null && value.startsWith("0x")) {
				groupItem.setGroupActionValue(Utils.decodeHexStr(value.substring(2, value.length())));
			}else if (value != null){
				throw new RuntimeException("Value: "+value+" in CodeXmlHandler dot start with " +
						"0x ");
			}
			dictionaryGroupItemStack.push(groupItem);
		}else if (qName.equals("item")) {
			DictionaryItem item = new DictionaryItem();
			item.setName(attributes.getValue("name"));
			String length = attributes.getValue("length");
			if (length != null) {
				item.setActionLength(Integer.parseInt(length));
			}
			String delimiter = attributes.getValue("delimiter");
			if (delimiter != null) {
				item.setDelimiter(attributes.getValue("delimiter").charAt(0));
			}
			String value = attributes.getValue("value");
			if (value != null && value.startsWith("0x")) {
				item.setActionValue(Utils.decodeHexStr(value.substring(2, value.length())));
			}else if (value != null){
				throw new RuntimeException("Value: "+value+" in CodeXmlHandler donot start with 0x ");
			}
			dictionaryItemsStack.push(item);
		}else if (qName.equals("field")) {
			DictionaryItemField field = new DictionaryItemField();
			dictionaryItemFieldsStack.push(field);
			field.setName(attributes.getValue("name"));
			String length = attributes.getValue("length");
			if (length != null) {
				field.setActionLength(Integer.parseInt(length));
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println(qNameStack.pop());
		if (qName.equals("dictionaries")) {
		}else if (qName.equals("dictionary")) {
			Dictionary dictionary = dictionaryStack.pop();
			data.add(dictionary);
			dictionary.groupItems.addAll(groupItems);
			groupItems.clear();
		}else if (qName.equals("group")) {
			DictionaryGroupItem groupItem = dictionaryGroupItemStack.pop();
			groupItem.items.addAll(items);
			groupItems.add(groupItem);
			items.clear();
		}else if (qName.equals("item")) {
			DictionaryItem item = dictionaryItemsStack.pop();
			items.add(item);
			item.fields.addAll(fields);
			fields.clear();
		}else if (qName.equals("field")) {
			DictionaryItemField field = dictionaryItemFieldsStack.pop();
			fields.add(field);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("Start Document");
		elementNode.add(data);
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("End Document");
		dictionaryStack = null;
		dictionaryItemsStack = null;
		dictionaryGroupItemStack = null;
		qNameStack = null;
		dictionaryItemFieldsStack = null;
	}

	public List<Dictionary> getNode() {
		if (this.elementNode.size() > 0 && this.elementNode.get(0) != null)
		return (List) this.elementNode.get(0);
		else
			return null;
	}
}
